const router = {
  navigate(params) {
    if (typeof params === 'string') params = { url: params }
    if (params.data) {
      params.url += '?'
      for (let key in params.data) {
        params.url += `${key}=${params.data[key]}&`
      }
      params.url = params.url.substring(0, params.url.length)
    }
    wx.navigateTo({
      url: params.url,
      success: params.resolve
    })
  },
  redirect(params) {
    if (typeof params === 'string') params = { url: params }
    if (params.data) {
      params.url += '?'
      for (let key in params.data) {
        params.url += `${key}=${params.data[key]}&`
      }
      params.url = params.url.substring(0, params.url.length)
    }
    wx.redirectTo({
      url: params.url,
      success: params.resolve
    })
  },
  back(num) {
    wx.navigateBack({
      delta: num,
    })
  },
  tab(params) {
    if (typeof params === 'string') params = { url: params }
    wx.switchTab({
      url: params.url,
      success: params.resolve
    })
  },
  launch(url) {
    wx.reLaunch({
      url: url
    })
  },

  tips(params) {
    if (typeof params === 'string') params = { content: params }
    wx.showToast({
      title: params.content || '哪里不对了~',
      icon: 'none',
      position: 'bottom',
      duration: params.time || 1500,
      success: params.resolve
    })
  }
}

export {
  router
};
